package net.wanho.productManage.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.productManage.dao.OrdersDetailDAO;
import net.wanho.productManage.po.OrdersDetail;
import net.wanho.systemSettings.po.Menu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrdersDetailDAOImpl extends BaseDAOImpl<OrdersDetail> implements OrdersDetailDAO {
    @Override
    public boolean insertBatch(List<OrdersDetail> ordersDetailList) throws DAOException {
        try {
            String sql = "INSERT INTO orders_detail (\n" +
                    "  DETAIL_ID,\n" +
                    "  ORDER_ID,\n" +
                    "  STORE_ID,\n" +
                    "  PRODUCT_ID,\n" +
                    "  BUY_NUMS,\n" +
                    "  REAL_PRICES,\n" +
                    "  REAL_TOTALPRICE\n" +
                    ") \n" +
                    "VALUES\n" +
                    "  (NULL, ?, ?, ?, ?, ?, ?)";
            Object[][] params = new Object[ordersDetailList.size()][6] ;
            for (int i = 0; i < ordersDetailList.size(); i++) {
                OrdersDetail ordersDetail = ordersDetailList.get(i);
                params[i][0] = ordersDetail.getOrders().getOrderId() ;
                params[i][1] = ordersDetail.getStore().getStoreId() ;
                params[i][2] = ordersDetail.getProduct().getProductId() ;
                params[i][3] = ordersDetail.getBuyNums();
                params[i][4] = ordersDetail.getRealPrices() ;
                params[i][5] = ordersDetail.getRealTotalprice() ;
            }
            return super.jdbcTemplate.updateBatch(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<OrdersDetail> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            //基础SQL
            String sql = "SELECT\n" +
                    "  DETAIL_ID detailId,\n" +
                    "  ORDER_ID \"orders.orderId\",\n" +
                    "  STORE_ID \"store.storeId\",\n" +
                    "  PRODUCT_ID \"product.productId\",\n" +
                    "  BUY_NUMS buyNums,\n" +
                    "  REAL_PRICES realPrices,\n" +
                    "  REAL_TOTALPRICE realTotalprice\n" +
                    "FROM orders_detail od\n" +
                    "WHERE 1=1 ";
            //动态拼接SQL
            StringBuffer sb = new StringBuffer(sql);
            //条件无效  或者 没有条件
            boolean isCond = condMap == null || condMap.isEmpty();
            //创建集合 存放 动态参数值 ？ 替换的值
            List<Object> condParamValueList = new ArrayList<>();
            //有效条件
            if (!isCond) {
                condMap.forEach((k, v) -> {
                    sb.append(" AND ").append(k).append(" =  ?");
                    condParamValueList.add(v);
                });
            }
            Object[] params = condParamValueList.toArray();
            return super.queryForList(sb.toString(), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
